﻿using System;
using System.Collections.Generic;
using System.Reflection;
using System.Resources;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Converter;
using csla4Skeleton.UI.SL.Resources.Utility;
using csla4Skeleton.UI.SL.DanhMuc.DM_NhaCungCap;

using csla4Skeleton.UI.SL.NghiepVu.MuaSanPham.DM_LoaiHoaDon;

using csla4Skeleton.UI.SL.NghiepVu.MuaSanPham.DM_LoaiTienTe;
using csla4Skeleton.UI.SL.DanhMuc.DM_HangHoa;
using csla4Skeleton.UI.SL.DanhMuc.DM_Kho;
using csla4Skeleton.UI.SL.DanhMuc.DM_TaiKhoan;
using csla4Skeleton.UI.SL.DanhMuc.DM_DonViTinh;
using csla4Skeleton.UI.SL.DanhMuc.DM_ThueSuat;
using csla4Skeleton.UI.SL.DanhMuc.DM_TinhChatHangHoa;
using csla4Skeleton.UI.SL.NghiepVu.BanSanPham.BSP_BanHangHoa;
using csla4Skeleton.UI.SL.NghiepVu.BanSanPham.BSP_BanDichVu;
using Accounting.Library;
using Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
using System.ComponentModel;
using Utility;
using Accounting.Library.Utility;

namespace csla4Skeleton.UI.SL.NghiepVu.TienMat.TM_PhieuThu
{
    public partial class TM_PhieuThu_MainWindow: UserControl, Csla.Xaml.ISupportNavigation
    {
        List<Control> controls = new List<Control>();
        #region ISupportNavigation Members

        public bool CreateBookmarkAfterLoadCompleted
        {
            get { return true; }
        }

        public event EventHandler LoadCompleted;

        protected virtual void OnLoadCompleted()
        {
            if (LoadCompleted != null)
                LoadCompleted(this, EventArgs.Empty);
        }

        public void SetParameters(string parameters)
        {
            try
            {
                if (parameters == "")
                    Khoa = null;
                else
                    Khoa = Int32.Parse(parameters);
            }
            catch
            {
                Khoa = null;
            }
        }

        public string Title
        {
            get { return "PhieuThu"; }
        }

        #endregion

        #region Load Methods
        private System.Nullable<int> Khoa = null;
      
        public TM_PhieuThu_MainWindow(int Khoa)          
        {
            this.Khoa = Khoa;
        }

        public TM_PhieuThu_MainWindow()
        {
            LocalizationConverter converter = new LocalizationConverter();
            converter.LocalResourceManager = new ResourceManager("csla4Skeleton.UI.SL.NghiepVu.TienMat.TM_PhieuThu.Resources", Assembly.GetExecutingAssembly());
            this.Resources.Add("Localization", converter);
            
            InitializeComponent();
            ControlBehavior.ControlBehaviors.Navigation(this.TM_PhieuThu_GeneralInformation_GroupBox, controls);
            ControlBehavior.ControlBehaviors.Navigation(this.TM_PhieuThu_MoreInformation_GroupBox, controls);
           
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                
                System.Windows.Data.CollectionViewSource TM_PhieuThu_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["TM_PhieuThu_ViewModelSource"];
                if (Khoa == null)
                {

                    TM_PhieuThu_ViewModel TM_PhieuThu_ViewModel = new TM_PhieuThu_ViewModel();
                    TM_PhieuThu_ViewModel.ChildList_AddedNew += new TM_PhieuThu_ViewModel.ChildList_AddedNew_EventHandler(TM_PhieuThu_ViewModel_ChildList_AddedNew);
                    TM_PhieuThu_ViewModel.TCHangHoaChanged += new EventHandler(TM_PhieuThu_TinhChatHangHoaChanged);
                    TM_PhieuThu_ViewModel.hangHoaIdChanged += new TM_PhieuThu_ViewModel.HangHoaIdChanged_EventHandler(TM_PhieuThu_HangHoaIdChanged);
                    TM_PhieuThu_ViewModel.LoaiTienTeChanged += new EventHandler(TM_PhieuThu_LoaiTienTeChanged);
                    TM_PhieuThu_ViewModel.ThueSuatIdChanged += new TM_PhieuThu_ViewModel.ThueSuatIdChanged_EventHandler(TM_PhieuThu_ThueSuatIdChanged);
                    TM_PhieuThu_CollectionViewSource.Source = new List<object> { TM_PhieuThu_ViewModel };
                    this.tabControl.Visibility = Visibility.Visible;
                    this.MoreInputGrid.Visibility = Visibility.Visible;

                    this.TM_PhieuThu_PhieuThuChiTietGridView.KeyboardCommandProvider = new ControlBehavior.GridViewKeyboardCommandProvider(this.TM_PhieuThu_PhieuThuChiTietGridView);
                    this.TM_PhieuThu_ThueChiTietGridView.KeyboardCommandProvider = new ControlBehavior.GridViewKeyboardCommandProvider(this.TM_PhieuThu_ThueChiTietGridView);

                    ((ControlBehavior.GridViewKeyboardCommandProvider)this.TM_PhieuThu_PhieuThuChiTietGridView.KeyboardCommandProvider).ReachLastColumn += new ControlBehavior.GridViewKeyboardCommandProvider.ReachLastColumn_EventHandler(OnReachLastColumn);
                    ((ControlBehavior.GridViewKeyboardCommandProvider)this.TM_PhieuThu_ThueChiTietGridView.KeyboardCommandProvider).ReachLastColumn += new ControlBehavior.GridViewKeyboardCommandProvider.ReachLastColumn_EventHandler(OnReachLastColumn);

                    ((ControlBehavior.GridViewKeyboardCommandProvider)this.TM_PhieuThu_PhieuThuChiTietGridView.KeyboardCommandProvider).TabKeyPress += new EventHandler(OnTabKeyPress);
                    ((ControlBehavior.GridViewKeyboardCommandProvider)this.TM_PhieuThu_ThueChiTietGridView.KeyboardCommandProvider).TabKeyPress += new EventHandler(OnTabKeyPress);
                }
                else
                {
                    TM_PhieuThu_ViewModel TM_PhieuThu_ViewModel = new TM_PhieuThu_ViewModel((int)Khoa);
                    TM_PhieuThu_ViewModel.ChildList_AddedNew += new TM_PhieuThu_ViewModel.ChildList_AddedNew_EventHandler(TM_PhieuThu_ViewModel_ChildList_AddedNew);
                    TM_PhieuThu_ViewModel.TCHangHoaChanged += new EventHandler(TM_PhieuThu_TinhChatHangHoaChanged);
                    TM_PhieuThu_ViewModel.hangHoaIdChanged += new TM_PhieuThu_ViewModel.HangHoaIdChanged_EventHandler(TM_PhieuThu_HangHoaIdChanged);
                    TM_PhieuThu_ViewModel.LoaiTienTeChanged += new EventHandler(TM_PhieuThu_LoaiTienTeChanged);
                    TM_PhieuThu_ViewModel.LoadCompleted += new EventHandler(TM_PhieuThu_ViewModel_LoadCompleted);
                    TM_PhieuThu_ViewModel.ThueSuatIdChanged += new TM_PhieuThu_ViewModel.ThueSuatIdChanged_EventHandler(TM_PhieuThu_ThueSuatIdChanged);

                    TM_PhieuThu_CollectionViewSource.Source = new List<object> { TM_PhieuThu_ViewModel };

                    
                }

                System.Windows.Data.CollectionViewSource dm_NhaCungCap_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_NhaCungCap_ListViewModelSource"];
                DM_NhaCungCap_ListViewModel dm_NhaCungCap_ListViewModel = new DM_NhaCungCap_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode, BusinessConfiguration.Configuration.DieuKienLocNCC_KhachHang);
                dm_NhaCungCap_CollectionViewSource.Source = new List<object> { dm_NhaCungCap_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_LoaiHoaDon_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_LoaiHoaDon_ListViewModelSource"];
                DM_LoaiHoaDon_ListViewModel dm_LoaiHoaDon_ListViewModel = new DM_LoaiHoaDon_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_LoaiHoaDon_CollectionViewSource.Source = new List<object> { dm_LoaiHoaDon_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_LoaiTienTe_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_LoaiTienTe_ListViewModelSource"];
                DM_LoaiTienTe_ListViewModel dm_LoaiTienTe_ListViewModel = new DM_LoaiTienTe_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_LoaiTienTe_CollectionViewSource.Source = new List<object> { dm_LoaiTienTe_ListViewModel };


                System.Windows.Data.CollectionViewSource dm_TaiKhoanNo_ChiTiet_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanNo_ChiTiet_ListViewModelSource"];
                DM_TaiKhoan_ListViewModel dm_TaiKhoanNo_ChiTiet_ListViewModel = new DM_TaiKhoan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_TaiKhoanNo_ChiTiet_CollectionViewSource.Source = new List<object> { dm_TaiKhoanNo_ChiTiet_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_TaiKhoanCo_ChiTiet_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanCo_ChiTiet_ListViewModelSource"];
                DM_TaiKhoan_ListViewModel dm_TaiKhoanCo_ChiTiet_ListViewModel = new DM_TaiKhoan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_TaiKhoanCo_ChiTiet_CollectionViewSource.Source = new List<object> { dm_TaiKhoanCo_ChiTiet_ListViewModel };


                System.Windows.Data.CollectionViewSource dm_HangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_HangHoa_ListViewModelSource"];
                DM_HangHoa_ListViewModel dm_HangHoa_ListViewModel = new DM_HangHoa_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_HangHoa_CollectionViewSource.Source = new List<object> { dm_HangHoa_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_TaiKhoanNo_Thue_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanNo_Thue_ListViewModelSource"];
                DM_TaiKhoan_ListViewModel dm_TaiKhoanNo_Thue_ListViewModel = new DM_TaiKhoan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_TaiKhoanNo_Thue_CollectionViewSource.Source = new List<object> { dm_TaiKhoanNo_Thue_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_TaiKhoanCo_Thue_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoanCo_Thue_ListViewModelSource"];
                DM_TaiKhoan_ListViewModel dm_TaiKhoanCo_Thue_ListViewModel = new DM_TaiKhoan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_TaiKhoanCo_Thue_CollectionViewSource.Source = new List<object> { dm_TaiKhoanCo_Thue_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_Kho_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_Kho_ListViewModelSource"];
                DM_Kho_ListViewModel dm_Kho_ListViewModel = new DM_Kho_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_Kho_CollectionViewSource.Source = new List<object> { dm_Kho_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_TaiKhoan_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoan_ListViewModelSource"];
                DM_TaiKhoan_ListViewModel dm_TaiKhoan_ListViewModel = new DM_TaiKhoan_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_TaiKhoan_CollectionViewSource.Source = new List<object> { dm_TaiKhoan_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_DonViTinh_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_DonViTinh_ListViewModelSource"];
                DM_DonViTinh_ListViewModel dm_DonViTinh_ListViewModel = new DM_DonViTinh_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_DonViTinh_CollectionViewSource.Source = new List<object> { dm_DonViTinh_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_ThueSuat_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_ThueSuat_ListViewModelSource"];
                DM_ThueSuat_ListViewModel dm_ThueSuat_ListViewModel = new DM_ThueSuat_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_ThueSuat_CollectionViewSource.Source = new List<object> { dm_ThueSuat_ListViewModel };

                System.Windows.Data.CollectionViewSource dm_TinhChatHangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TinhChatHangHoa_ListViewModelSource"];
                DM_TinhChatHangHoa_ListViewModel dm_TinhChatHangHoa_ListViewModel = new DM_TinhChatHangHoa_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                dm_TinhChatHangHoa_CollectionViewSource.Source = new List<object> { dm_TinhChatHangHoa_ListViewModel };

               
            }
        }

        #endregion
       

        #region Event Handle

        private void LayoutRoot_KeyUp(object sender, KeyEventArgs e)
        {
            if (Keyboard.Modifiers == ModifierKeys.Control)
            {
                TM_PhieuThu_ViewModel vm = GetViewModel();
                if (e.Key == Key.S)
                {
                    if (vm.CanSave)
                    {
                        vm.Save(sender, new Csla.Xaml.ExecuteEventArgs());
                    }
                }
                else if (e.Key == Key.F6)
                {
                    AddItems();
                }
                else if (e.Key == Key.D1)
                {
                    this.TM_PhieuThu_PhieuThuChiTietTab.IsSelected = true;
                }
                else if (e.Key == Key.D2)
                {
                    this.TM_PhieuThu_ThueChiTietTab.IsSelected = true;
                }

            }
            
            e.Handled = true;
        }

        
       
           

        void AddItemButton_Click(object sender, RoutedEventArgs e)
        {

            AddItems();
        }

        void DeleteItemButton_Click(object sender, RoutedEventArgs e)
        {
            DeleteItems();
          
        }

        public void AddItems()
        {
            if (this.tabControl.Visibility == Visibility.Visible)
            {
                if (this.TM_PhieuThu_PhieuThuChiTietTab.IsSelected)
                {
                    AddPhieuThuDetailsItem();
                }
            }
            else if (this.BanHangHoaTabControl.Visibility == Visibility.Visible)
            {
                if (this.BSP_BanHangHoa_BanHangHoaChiTietTab.IsSelected)
                {
                    AddBanHangHoaDetailsItem();
                }
            }
            else if (this.BanDichVuTabControl.Visibility == Visibility.Visible)
            {
                if (this.BSP_BanDichVu_BanDichVuChiTietTab.IsSelected)
                {
                    AddBanDichVuDetailsItem();
                }
            }
        }
        private RadGridView GetActiveGridView()
        {
            if (this.TM_PhieuThu_PhieuThuChiTietTab.IsSelected)
            {
                return TM_PhieuThu_PhieuThuChiTietGridView;
            }
            else if (this.TM_PhieuThu_ThueChiTietTab.IsSelected)
            {
                return TM_PhieuThu_ThueChiTietGridView;
            }
            else if (this.BSP_BanHangHoa_BanHangHoaChiTietTab.IsSelected)
            {
                return BSP_BanHangHoa_BanHangHoaChiTietGridView;
            }
            else if (this.BSP_BanHangHoa_ThueChiTietTab.IsSelected)
            {
                return BSP_BanHangHoa_ThueChiTietGridView;
            }
            else if (this.BSP_BanHangHoa_PhuPhiTab.IsSelected)
            {
                return BSP_BanHangHoa_PhuPhiGridView;
            }
            else if (this.BSP_BanHangHoa_GiaVonTab.IsSelected)
            {
                return BSP_BanHangHoa_GiaVonGridView;
            }
            else if (this.BSP_BanDichVu_BanDichVuChiTietTab.IsSelected)
            {
                return BSP_BanDichVu_BanDichVuChiTietGridView;
            }
            else if (this.BSP_BanDichVu_ThueChiTietTab.IsSelected)
            {
                return BSP_BanDichVu_ThueChiTietGridView;
            }
            else if (this.BSP_BanDichVu_PhuPhiTab.IsSelected)
            {
                return BSP_BanDichVu_PhuPhiGridView;
            }
            return null;
        }



        void DeleteItems()
        {
            if (this.tabControl.Visibility == Visibility.Visible)
            {
                if (this.TM_PhieuThu_PhieuThuChiTietTab.IsSelected)
                {
                    TM_PhieuThu_ChiTietChild ChiTietChild = (TM_PhieuThu_ChiTietChild)(this.TM_PhieuThu_PhieuThuChiTietGridView.SelectedItem);
                    IList<TM_PhieuThu_ChiTietChild> list = GetViewModel().Model.TM_PhieuThu_ChiTiets;
                    int index = list.IndexOf(ChiTietChild);
                    DeletePhieuThuDetailsItem(index);
                }
            }
            else if (this.BanHangHoaTabControl.Visibility == Visibility.Visible)
            {
                if (this.BSP_BanHangHoa_BanHangHoaChiTietTab.IsSelected)
                {
                    BSP_BanHangHoa_ChiTietChild ChiTietChild = (BSP_BanHangHoa_ChiTietChild)(this.BSP_BanHangHoa_BanHangHoaChiTietGridView.SelectedItem);
                    IList<BSP_BanHangHoa_ChiTietChild> list = this.GetBanHangHoaViewModel().Model.BSP_BanHangHoa_ChiTiets;
                    int index = list.IndexOf(ChiTietChild);
                    DeleteBanHangHoaDetailsItem(index);
                }
            }
            else if (this.BanDichVuTabControl.Visibility == Visibility.Visible)
            {
                if (this.BSP_BanDichVu_BanDichVuChiTietTab.IsSelected)
                {
                    BSP_BanDichVu_ChiTietChild ChiTietChild = (BSP_BanDichVu_ChiTietChild)(this.BSP_BanDichVu_BanDichVuChiTietGridView.SelectedItem);
                    IList<BSP_BanDichVu_ChiTietChild> list = this.GetBanDichVuViewModel().Model.BSP_BanDichVu_ChiTiets;
                    int index = list.IndexOf(ChiTietChild);
                    DeleteBanDichVuDetailsItem(index);
                }
            }

        }
           
        void GridView_AddingNewDataItem(object sender, GridViewAddingNewEventArgs e)
        {
            var grid = e.OwnerGridViewItemsControl;
            if (grid.Items.Count > 0)
            {
                GridViewUtil.FocusRow((RadGridView)grid, grid.Items.Count - 1);
                Dispatcher.BeginInvoke(new Action(() =>
                {
                    grid.CurrentCellInfo = new GridViewCellInfo(grid.Items[grid.Items.Count - 1], grid.Columns[0]);
                    grid.Focus();
                    grid.BeginEdit();
                }));
            }
        }

        private void SaveAndNewButton_Click(object sender, RoutedEventArgs e)
        {
            if (this.tabControl.Visibility == Visibility.Visible)
            {
                this.GetViewModel().SaveAndNew();
            }
            else if (this.BanHangHoaTabControl.Visibility == Visibility.Visible)
            {
                this.GetBanHangHoaViewModel().SaveAndNew();
            }
            else if (this.BanDichVuTabControl.Visibility == Visibility.Visible)
            {
                this.GetBanDichVuViewModel().SaveAndNew();
            }
        }

        private void DatePicker_KeyUp(object sender, KeyEventArgs e)
        {
            List<Control> availableControl = new List<Control>();
            foreach (Control item in controls)
            {
                if (item.Visibility == System.Windows.Visibility.Visible)
                {
                    availableControl.Add(item);
                }
            }
            Control[] list = availableControl.ToArray();
            if (sender.GetHashCode() == list[availableControl.Count - 1].GetHashCode())
            {
                if (e.Key == Key.Enter)
                {
                    if (this.tabControl.Visibility == Visibility.Visible)
                    {
                        this.AddPhieuThuDetailsItem();
                    }
                    else if (this.BanHangHoaTabControl.Visibility == Visibility.Visible)
                    {

                        this.AddBanHangHoaDetailsItem();
                    }
                    else if (this.BanDichVuTabControl.Visibility == Visibility.Visible)
                    {
                        this.AddBanDichVuDetailsItem();
                    }
                }
            }
            else
            {
                ControlBehavior.DatePickerBehaviors.DatePicker_KeyUp(sender, e, controls);
            }
        }

        private void ComboBox_KeyUp(object sender, KeyEventArgs e)
        {
            List<Control> availableControl = new List<Control>();
            foreach (Control item in controls)
            {
                if (item.Visibility == System.Windows.Visibility.Visible)
                {
                    availableControl.Add(item);
                }
            }
            Control[] list = availableControl.ToArray();
            if (sender.GetHashCode() == list[availableControl.Count - 1].GetHashCode())
            {
                if (e.Key == Key.Enter)
                {
                    if (this.tabControl.Visibility == Visibility.Visible)
                    {
                        this.AddPhieuThuDetailsItem();
                    }
                    else if (this.BanHangHoaTabControl.Visibility == Visibility.Visible)
                    {

                        this.AddBanHangHoaDetailsItem();
                    }
                    else if (this.BanDichVuTabControl.Visibility == Visibility.Visible)
                    {
                        this.AddBanDichVuDetailsItem();
                    }
                }
            }
            else
            {
                ControlBehavior.ComboBoxBehaviors.ComboBox_KeyUp(sender, e, controls);
            }
        }

        private void Control_KeyUp(object sender, KeyEventArgs e)
        {
            List<Control> availableControl = new List<Control>();
            foreach (Control item in controls)
            {
                if (item.Visibility == System.Windows.Visibility.Visible)
                {
                    availableControl.Add(item);
                }
            }
            Control[] list = availableControl.ToArray();
            if (sender.GetHashCode() == list[availableControl.Count - 1].GetHashCode())
            {
                if (e.Key == Key.Enter)
                {
                    if (this.tabControl.Visibility == Visibility.Visible)
                    {
                        this.AddPhieuThuDetailsItem();
                    }
                    else if (this.BanHangHoaTabControl.Visibility == Visibility.Visible)
                    {

                        this.AddBanHangHoaDetailsItem();
                    }
                    else if (this.BanDichVuTabControl.Visibility == Visibility.Visible)
                    {
                        this.AddBanDichVuDetailsItem();
                    }
                }
            }
            else
            {
                ControlBehavior.ControlBehaviors.Control_KeyUp(sender, e, controls);
            }
        }

        private void BookedItemButton_Click(object sender, RoutedEventArgs e)
        {
            GetViewModel().GhiSo();
        }

        private void DeleteBookedItemButton_Click(object sender, RoutedEventArgs e)
        {
            GetViewModel().BoGhi();
        }

        void OnTabKeyPress(object sender, EventArgs e)
        {
            if (this.tabControl.Visibility == Visibility.Visible)
            {
                this.tabControl.SelectedItem = tabControl.Items[0];
                this.AddPhieuThuDetailsItem();
            }
            if (this.BanHangHoaTabControl.Visibility == Visibility.Visible)
            {
                this.BanHangHoaTabControl.SelectedItem = BanHangHoaTabControl.Items[0];
                this.AddBanHangHoaDetailsItem();
            }
            else if (this.BanDichVuTabControl.Visibility == Visibility.Visible)
            {
                this.BanDichVuTabControl.SelectedItem = BanDichVuTabControl.Items[0];
                this.AddBanDichVuDetailsItem();
            }
        }

        void OnReachLastColumn(object sender, ReachLastColumnArgs e)
        {

            if (tabControl.Visibility == Visibility.Visible)
            {
                List<TabItem> AvailableTabItem = new List<TabItem>();
                int indexCurrentTab = 0;
                foreach (TabItem item in this.tabControl.Items)
                {
                    if (item.Visibility == System.Windows.Visibility.Visible)
                    {
                        AvailableTabItem.Add(item);
                        if (item.GetHashCode() == tabControl.SelectedItem.GetHashCode())
                        {
                            indexCurrentTab = AvailableTabItem.IndexOf(item);
                        }

                    }
                }

                TabItem[] list = AvailableTabItem.ToArray();
                if (indexCurrentTab == (AvailableTabItem.Count - 1))
                {
                    this.tabControl.SelectedItem = list[0];
                    FocusAfterChangeTab(this.TM_PhieuThu_PhieuThuChiTietGridView, e.Index);

                }
                else
                {
                    this.tabControl.SelectedItem = list[indexCurrentTab + 1];
                    if (list[indexCurrentTab + 1].Name.Equals("TM_PhieuThu_ThueChiTietTab"))
                    {
                        FocusAfterChangeTab(this.TM_PhieuThu_ThueChiTietGridView, e.Index);
                    }
                }
            }
            else if (BanHangHoaTabControl.Visibility == Visibility.Visible)
            {
                List<TabItem> AvailableTabItem = new List<TabItem>();
                int indexCurrentTab = 0;
                foreach (TabItem item in this.BanHangHoaTabControl.Items)
                {
                    if (item.Visibility == System.Windows.Visibility.Visible)
                    {
                        AvailableTabItem.Add(item);
                        if (item.GetHashCode() == BanHangHoaTabControl.SelectedItem.GetHashCode())
                        {
                            indexCurrentTab = AvailableTabItem.IndexOf(item);
                        }

                    }
                }


                TabItem[] list = AvailableTabItem.ToArray();
                if (indexCurrentTab == (AvailableTabItem.Count - 1))
                {
                    this.BanHangHoaTabControl.SelectedItem = list[0];
                    FocusAfterChangeTab(this.BSP_BanHangHoa_BanHangHoaChiTietGridView, e.Index);

                }
                else
                {
                    this.BanHangHoaTabControl.SelectedItem = list[indexCurrentTab + 1];

                    if (list[indexCurrentTab + 1].Name.Equals("BSP_BanHangHoa_PhuPhiTab"))
                    {
                        FocusAfterChangeTab(this.BSP_BanHangHoa_PhuPhiGridView, e.Index);
                    }
                    else if (list[indexCurrentTab + 1].Name.Equals("BSP_BanHangHoa_ThueChiTietTab"))
                    {
                        FocusAfterChangeTab(this.BSP_BanHangHoa_ThueChiTietGridView, e.Index);

                    }
                    else if (list[indexCurrentTab + 1].Name.Equals("BSP_BanHangHoa_GiaVonTab"))
                    {
                        FocusAfterChangeTab(this.BSP_BanHangHoa_GiaVonGridView, e.Index);
                    }

                }
            }
            else if (BanDichVuTabControl.Visibility == Visibility.Visible)
            {
                List<TabItem> AvailableTabItem = new List<TabItem>();
                int indexCurrentTab = 0;
                foreach (TabItem item in this.BanDichVuTabControl.Items)
                {
                    if (item.Visibility == System.Windows.Visibility.Visible)
                    {
                        AvailableTabItem.Add(item);
                        if (item.GetHashCode() == BanDichVuTabControl.SelectedItem.GetHashCode())
                        {
                            indexCurrentTab = AvailableTabItem.IndexOf(item);
                        }

                    }
                }


                TabItem[] list = AvailableTabItem.ToArray();
                if (indexCurrentTab == (AvailableTabItem.Count - 1))
                {
                    this.BanDichVuTabControl.SelectedItem = list[0];
                    FocusAfterChangeTab(this.BSP_BanDichVu_BanDichVuChiTietGridView, e.Index);
                }
                else
                {
                    this.BanDichVuTabControl.SelectedItem = list[indexCurrentTab + 1];
                    if (list[indexCurrentTab + 1].Name.Equals("BSP_BanDichVu_PhuPhiTab"))
                    {
                        FocusAfterChangeTab(this.BSP_BanDichVu_PhuPhiGridView, e.Index);
                    }
                    else if (list[indexCurrentTab + 1].Name.Equals("BSP_BanDichVu_ThueChiTietTab"))
                    {
                        FocusAfterChangeTab(this.BSP_BanDichVu_ThueChiTietGridView, e.Index);

                    }
                }
            }

        }

        private void FocusAfterChangeTab(GridViewDataControl grid, int index)
        {
            
                Dispatcher.BeginInvoke(new Action(() =>
                {
                    grid.CurrentCellInfo = new GridViewCellInfo(grid.Items[index], grid.Columns[0]);
                    grid.Focus();
                    grid.BeginEdit();
                }));
           
        }
        private void TM_PhieuThu_GeneralInformation_NhaCungCapId_AddItemButton_Click(object sender, RoutedEventArgs e)
        {
            DM_NhaCungCap_ChildWindow dm_NhaCungCap_ChildWindow = new DM_NhaCungCap_ChildWindow();
            dm_NhaCungCap_ChildWindow.Show();
            dm_NhaCungCap_ChildWindow.SaveClicked += new EventHandler(dm_NhaCungCap_ViewModel_Saved);
        }

        

        private void TM_PhieuThu_MoreInformation_LoaiTienId_AddItemButton_Click(object sender, RoutedEventArgs e)
        {
            DM_LoaiTienTe_ChildWindow dm_LoaiTienTe_ChildWindow = new DM_LoaiTienTe_ChildWindow();
            dm_LoaiTienTe_ChildWindow.Show();
            dm_LoaiTienTe_ChildWindow.SaveClicked += new EventHandler(dm_LoaiTienTe_ViewModel_Saved);
        }

       
        #endregion

        #region View Model Related Methods
        private TM_PhieuThu_ViewModel GetViewModel()
        {
            System.Windows.Data.CollectionViewSource TM_PhieuThu_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["TM_PhieuThu_ViewModelSource"];
            return ((List<object>)TM_PhieuThu_CollectionViewSource.Source)[0] as TM_PhieuThu_ViewModel;
        }

        private BSP_BanHangHoa_ViewModel GetBanHangHoaViewModel()
        {
            System.Windows.Data.CollectionViewSource BSP_BanHangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["BSP_BanHangHoa_ViewModelSource"];
            return ((List<object>)BSP_BanHangHoa_CollectionViewSource.Source)[0] as BSP_BanHangHoa_ViewModel;
        }

        private BSP_BanDichVu_ViewModel GetBanDichVuViewModel()
        {
            System.Windows.Data.CollectionViewSource BSP_BanDichVu_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["BSP_BanDichVu_ViewModelSource"];
            return ((List<object>)BSP_BanDichVu_CollectionViewSource.Source)[0] as BSP_BanDichVu_ViewModel;
        }
        void dm_NhaCungCap_ViewModel_Saved(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_NhaCungCap_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_NhaCungCap_ListViewModelSource"];
            DM_NhaCungCap_ListViewModel vm = ((List<object>)dm_NhaCungCap_ListViewSource.Source)[0] as DM_NhaCungCap_ListViewModel;
            vm.GetDM_NhaCungCap_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode, BusinessConfiguration.Configuration.DieuKienLocNCC_KhachHang);
        }


        public void TM_PhieuThu_ViewModel_LoadCompleted(object sender, EventArgs e)
        {
            if (this.GetViewModel().Model.ChungTuThayTheId == null)
            {
                this.tabControl.Visibility = Visibility.Visible;
                this.MoreInputGrid.Visibility = Visibility.Visible;

                this.GetViewModel().Model.TongTien = this.GetViewModel().Model.TongTienQĐ / this.GetViewModel().Model.TyGia;
                this.GetViewModel().Model.TienThueGTGT = this.GetViewModel().Model.TienThueGTGTQĐ / this.GetViewModel().Model.TyGia;
                this.GetViewModel().Model.TongTienThanhToan = this.GetViewModel().Model.TongTienThanhToanQĐ / this.GetViewModel().Model.TyGia;

                this.TM_PhieuThu_PhieuThuChiTietGridView.KeyboardCommandProvider = new ControlBehavior.GridViewKeyboardCommandProvider(this.TM_PhieuThu_PhieuThuChiTietGridView);
                this.TM_PhieuThu_ThueChiTietGridView.KeyboardCommandProvider = new ControlBehavior.GridViewKeyboardCommandProvider(this.TM_PhieuThu_ThueChiTietGridView);

                ((ControlBehavior.GridViewKeyboardCommandProvider)this.TM_PhieuThu_PhieuThuChiTietGridView.KeyboardCommandProvider).ReachLastColumn += new ControlBehavior.GridViewKeyboardCommandProvider.ReachLastColumn_EventHandler(OnReachLastColumn);
                ((ControlBehavior.GridViewKeyboardCommandProvider)this.TM_PhieuThu_ThueChiTietGridView.KeyboardCommandProvider).ReachLastColumn += new ControlBehavior.GridViewKeyboardCommandProvider.ReachLastColumn_EventHandler(OnReachLastColumn);

                ((ControlBehavior.GridViewKeyboardCommandProvider)this.TM_PhieuThu_PhieuThuChiTietGridView.KeyboardCommandProvider).TabKeyPress += new EventHandler(OnTabKeyPress);
                ((ControlBehavior.GridViewKeyboardCommandProvider)this.TM_PhieuThu_ThueChiTietGridView.KeyboardCommandProvider).TabKeyPress += new EventHandler(OnTabKeyPress);

            }
            else if (this.GetViewModel().Model.LoaiChungTuThayTheId == 401)
            {
                this.BanHangHoaTabControl.Visibility = Visibility.Visible;

                System.Windows.Data.CollectionViewSource bsp_BanHangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["BSP_BanHangHoa_ViewModelSource"];
                BSP_BanHangHoa_ViewModel bsp_BanHangHoa_ViewModel = new BSP_BanHangHoa_ViewModel((int)this.GetViewModel().Model.ChungTuThayTheId);
                bsp_BanHangHoa_ViewModel.ChildList_AddedNew += new BSP_BanHangHoa_ViewModel.ChildList_AddedNew_EventHandler(bsp_BanHangHoa_ViewModel_ChildList_AddedNew);
                bsp_BanHangHoa_ViewModel.LoadCompleted += new EventHandler(bsp_BanHangHoa_ViewModel_LoadCompleted);
                bsp_BanHangHoa_ViewModel.hangHoaIdChanged += new BSP_BanHangHoa_ViewModel.HangHoaIdChanged_EventHandler(bsp_BanHangHoa_HangHoaIdChanged);
                bsp_BanHangHoa_ViewModel.ThueSuatIdChanged += new BSP_BanHangHoa_ViewModel.ThueSuatIdChanged_EventHandler(bsp_BanHangHoa_ThueSuatIdChanged);
                bsp_BanHangHoa_ViewModel.TongTienThanhToanChanged += new EventHandler(OnTongTienThanhToanChanged);
                bsp_BanHangHoa_ViewModel.TongTienHangChanged += new EventHandler(OnTongTienHangChanged);
                bsp_BanHangHoa_ViewModel.TienThueGTGTChanged += new EventHandler(OnTienThueGTGTChanged);
                bsp_BanHangHoa_CollectionViewSource.Source = new List<object> { bsp_BanHangHoa_ViewModel };

                this.BSP_BanHangHoa_BanHangHoaChiTietGridView.KeyboardCommandProvider = new ControlBehavior.GridViewKeyboardCommandProvider(this.BSP_BanHangHoa_BanHangHoaChiTietGridView);
                this.BSP_BanHangHoa_ThueChiTietGridView.KeyboardCommandProvider = new ControlBehavior.GridViewKeyboardCommandProvider(this.BSP_BanHangHoa_ThueChiTietGridView);
                this.BSP_BanHangHoa_PhuPhiGridView.KeyboardCommandProvider = new ControlBehavior.GridViewKeyboardCommandProvider(this.BSP_BanHangHoa_PhuPhiGridView);
                this.BSP_BanHangHoa_GiaVonGridView.KeyboardCommandProvider = new ControlBehavior.GridViewKeyboardCommandProvider(this.BSP_BanHangHoa_GiaVonGridView);

                ((ControlBehavior.GridViewKeyboardCommandProvider)this.BSP_BanHangHoa_BanHangHoaChiTietGridView.KeyboardCommandProvider).ReachLastColumn += new ControlBehavior.GridViewKeyboardCommandProvider.ReachLastColumn_EventHandler(OnReachLastColumn);
                ((ControlBehavior.GridViewKeyboardCommandProvider)this.BSP_BanHangHoa_ThueChiTietGridView.KeyboardCommandProvider).ReachLastColumn += new ControlBehavior.GridViewKeyboardCommandProvider.ReachLastColumn_EventHandler(OnReachLastColumn);
                ((ControlBehavior.GridViewKeyboardCommandProvider)this.BSP_BanHangHoa_PhuPhiGridView.KeyboardCommandProvider).ReachLastColumn += new ControlBehavior.GridViewKeyboardCommandProvider.ReachLastColumn_EventHandler(OnReachLastColumn);
                ((ControlBehavior.GridViewKeyboardCommandProvider)this.BSP_BanHangHoa_GiaVonGridView.KeyboardCommandProvider).ReachLastColumn += new ControlBehavior.GridViewKeyboardCommandProvider.ReachLastColumn_EventHandler(OnReachLastColumn);

                ((ControlBehavior.GridViewKeyboardCommandProvider)this.BSP_BanHangHoa_BanHangHoaChiTietGridView.KeyboardCommandProvider).TabKeyPress += new EventHandler(OnTabKeyPress);
                ((ControlBehavior.GridViewKeyboardCommandProvider)this.BSP_BanHangHoa_ThueChiTietGridView.KeyboardCommandProvider).TabKeyPress += new EventHandler(OnTabKeyPress);
                ((ControlBehavior.GridViewKeyboardCommandProvider)this.BSP_BanHangHoa_PhuPhiGridView.KeyboardCommandProvider).TabKeyPress += new EventHandler(OnTabKeyPress);
                ((ControlBehavior.GridViewKeyboardCommandProvider)this.BSP_BanHangHoa_GiaVonGridView.KeyboardCommandProvider).TabKeyPress += new EventHandler(OnTabKeyPress);

            }
            else if (this.GetViewModel().Model.LoaiChungTuThayTheId == 402)
            {
                this.BanDichVuTabControl.Visibility = Visibility.Visible;

                //System.Windows.Data.CollectionViewSource bsp_BanDichVu_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["BSP_BanDichVu_ViewModelSource"];
                //BSP_BanDichVu_ViewModel bsp_BanDichVu_ViewModel = new BSP_BanDichVu_ViewModel((int)this.GetViewModel().Model.ChungTuThayTheId);
                //bsp_BanDichVu_ViewModel.ChildList_AddedNew += new BSP_BanDichVu_ViewModel.ChildList_AddedNew_EventHandler(bsp_BanDichVu_ViewModel_ChildList_AddedNew);
                //bsp_BanDichVu_ViewModel.LoadCompleted += new EventHandler(bsp_BanDichVu_ViewModel_LoadCompleteted);
                //bsp_BanDichVu_ViewModel.ThueSuatIdChanged += new BSP_BanDichVu_ViewModel.ThueSuatIdChanged_EventHandler(bsp_BanDichVu_ThueSuatIdChanged);
                //bsp_BanDichVu_ViewModel.hangHoaIdChanged += new BSP_BanDichVu_ViewModel.HangHoaIdChanged_EventHandler(bsp_BanDichVu_HangHoaIdChanged);
                //bsp_BanDichVu_ViewModel.TongTienThanhToanChanged += new EventHandler(OnTongTienThanhToanChanged);
                //bsp_BanDichVu_ViewModel.TongTienHangChanged += new EventHandler(OnTongTienHangChanged);
                //bsp_BanDichVu_ViewModel.TienThueGTGTChanged += new EventHandler(OnTienThueGTGTChanged);
                //bsp_BanDichVu_CollectionViewSource.Source = new List<object> { bsp_BanDichVu_ViewModel };

                //this.BSP_BanDichVu_BanDichVuChiTietGridView.KeyboardCommandProvider = new ControlBehavior.GridViewKeyboardCommandProvider(this.BSP_BanDichVu_BanDichVuChiTietGridView);
                //this.BSP_BanDichVu_ThueChiTietGridView.KeyboardCommandProvider = new ControlBehavior.GridViewKeyboardCommandProvider(this.BSP_BanDichVu_ThueChiTietGridView);
                //this.BSP_BanDichVu_PhuPhiGridView.KeyboardCommandProvider = new ControlBehavior.GridViewKeyboardCommandProvider(this.BSP_BanDichVu_PhuPhiGridView);

                //((ControlBehavior.GridViewKeyboardCommandProvider)this.BSP_BanDichVu_BanDichVuChiTietGridView.KeyboardCommandProvider).ReachLastColumn += new ControlBehavior.GridViewKeyboardCommandProvider.ReachLastColumn_EventHandler(OnReachLastColumn);
                //((ControlBehavior.GridViewKeyboardCommandProvider)this.BSP_BanDichVu_ThueChiTietGridView.KeyboardCommandProvider).ReachLastColumn += new ControlBehavior.GridViewKeyboardCommandProvider.ReachLastColumn_EventHandler(OnReachLastColumn);
                //((ControlBehavior.GridViewKeyboardCommandProvider)this.BSP_BanDichVu_PhuPhiGridView.KeyboardCommandProvider).ReachLastColumn += new ControlBehavior.GridViewKeyboardCommandProvider.ReachLastColumn_EventHandler(OnReachLastColumn);


                //((ControlBehavior.GridViewKeyboardCommandProvider)this.BSP_BanDichVu_BanDichVuChiTietGridView.KeyboardCommandProvider).TabKeyPress += new EventHandler(OnTabKeyPress);
                //((ControlBehavior.GridViewKeyboardCommandProvider)this.BSP_BanDichVu_ThueChiTietGridView.KeyboardCommandProvider).TabKeyPress += new EventHandler(OnTabKeyPress);
                //((ControlBehavior.GridViewKeyboardCommandProvider)this.BSP_BanDichVu_PhuPhiGridView.KeyboardCommandProvider).TabKeyPress += new EventHandler(OnTabKeyPress);
               
               
            }
        }

        private void OnTongTienHangChanged(object sender, EventArgs e)
        {
            if (this.BanHangHoaTabControl.Visibility == Visibility.Visible)
            {
                this.GetViewModel().Model.TongTien = this.GetBanHangHoaViewModel().Model.TongTienHang;
                this.GetViewModel().Model.TongTienQĐ = this.GetBanHangHoaViewModel().Model.TongTienHang * GetBanHangHoaViewModel().Model.TyGia;
            }
            else if (this.BanDichVuTabControl.Visibility == Visibility.Visible)
            {
                this.GetViewModel().Model.TongTien = this.GetBanDichVuViewModel().Model.TongTienHang;
                this.GetViewModel().Model.TongTienQĐ = this.GetBanDichVuViewModel().Model.TongTienHang * GetBanDichVuViewModel().Model.TyGia;
            }
        }

        private void OnTienThueGTGTChanged(object sender, EventArgs e)
        {
            if (this.BanHangHoaTabControl.Visibility == Visibility.Visible)
            {
                this.GetViewModel().Model.TienThueGTGT = this.GetBanHangHoaViewModel().Model.TienThueGTGT;
                this.GetViewModel().Model.TienThueGTGTQĐ = this.GetBanHangHoaViewModel().Model.TienThueGTGT * GetBanHangHoaViewModel().Model.TyGia;
            }
            else if (this.BanDichVuTabControl.Visibility == Visibility.Visible)
            {
                this.GetViewModel().Model.TienThueGTGT = this.GetBanDichVuViewModel().Model.TienThueGTGT;
                this.GetViewModel().Model.TienThueGTGTQĐ = this.GetBanDichVuViewModel().Model.TienThueGTGT * GetBanDichVuViewModel().Model.TyGia;
            }
        }

        private void OnTongTienThanhToanChanged(object sender, EventArgs e)
        {
            if (this.BanHangHoaTabControl.Visibility == Visibility.Visible)
            {
                this.GetViewModel().Model.TongTienThanhToan = this.GetBanHangHoaViewModel().Model.TongTienThanhToan;
                this.GetViewModel().Model.TongTienThanhToanQĐ = this.GetBanHangHoaViewModel().Model.TongTienThanhToan * GetBanHangHoaViewModel().Model.TyGia;
            }
            else if (this.BanDichVuTabControl.Visibility == Visibility.Visible)
            {
                this.GetViewModel().Model.TongTienThanhToan = this.GetBanDichVuViewModel().Model.TongTienThanhToan;
                this.GetViewModel().Model.TongTienThanhToanQĐ = this.GetBanDichVuViewModel().Model.TongTienThanhToan * GetBanDichVuViewModel().Model.TyGia;
            }
        }

        private void bsp_BanHangHoa_ViewModel_LoadCompleted(object sender, EventArgs e)
        {
           
            this.GetViewModel().Model.TongTien = this.GetViewModel().Model.TongTienQĐ / GetBanHangHoaViewModel().Model.TyGia;
            this.GetViewModel().Model.TienThueGTGT = this.GetViewModel().Model.TienThueGTGTQĐ / GetBanHangHoaViewModel().Model.TyGia;
            this.GetViewModel().Model.TongTienThanhToan = this.GetViewModel().Model.TongTienThanhToanQĐ / GetBanHangHoaViewModel().Model.TyGia;


            if (GetBanHangHoaViewModel().Model.LoaiTienId == 3)
            {
                this.BSP_BanHangHoa_BanHangHoaChiTietGridView.Columns[9].IsVisible = false;
                this.BSP_BanHangHoa_BanHangHoaChiTietGridView.Columns[11].IsVisible = false;
                this.BSP_BanHangHoa_BanHangHoaChiTietGridView.Columns[14].IsVisible = false;

                this.BSP_BanHangHoa_ThueChiTietGridView.Columns[4].IsVisible = false;
                this.BSP_BanHangHoa_PhuPhiGridView.Columns[3].IsVisible = false;
                this.BSP_BanHangHoa_GiaVonGridView.Columns[5].IsVisible = false;
                this.BSP_BanHangHoa_GiaVonGridView.Columns[7].IsVisible = false;
            }
        }
        private void bsp_BanDichVu_ViewModel_LoadCompleteted(object sender, EventArgs e)
        {
           
            this.GetViewModel().Model.TongTien = this.GetViewModel().Model.TongTienQĐ / GetBanDichVuViewModel().Model.TyGia;
            this.GetViewModel().Model.TienThueGTGT = this.GetViewModel().Model.TienThueGTGTQĐ / GetBanDichVuViewModel().Model.TyGia;
            this.GetViewModel().Model.TongTienThanhToan = this.GetViewModel().Model.TongTienThanhToanQĐ / GetBanDichVuViewModel().Model.TyGia;

            if (GetBanDichVuViewModel().Model.LoaiTienId == 3)
            {
                this.BSP_BanDichVu_BanDichVuChiTietGridView.Columns[8].IsVisible = false;
                this.BSP_BanDichVu_BanDichVuChiTietGridView.Columns[11].IsVisible = false;
                this.BSP_BanDichVu_ThueChiTietGridView.Columns[3].IsVisible = false;
                this.BSP_BanDichVu_PhuPhiGridView.Columns[2].IsVisible = false;

            }
        }

        void bsp_BanHangHoa_ThueSuatIdChanged(object sender, ThueSuatIdChangedEventArgs e)
        {

            System.Windows.Data.CollectionViewSource dm_ThueSuat_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_ThueSuat_ListViewModelSource"];
            DM_ThueSuat_ListViewModel dm_ThueSuat_ListViewModel = ((List<object>)dm_ThueSuat_CollectionViewSource.Source)[0] as DM_ThueSuat_ListViewModel;

            BSP_BanHangHoa_ThueChild ThueChild = GetBanHangHoaViewModel().Model.BSP_BanHangHoa_Thues[e.Index];
            IList<DM_ThueSuatInfo> list = dm_ThueSuat_ListViewModel.Model.getListOfInfo();
            foreach (DM_ThueSuatInfo item in list)
            {
                if (item.Khoa == e.ThueSuatId)
                {
                    ThueChild.ThueSuatValue = item.GiaTriTinhThue;
                }
            }

        }
        void bsp_BanDichVu_ThueSuatIdChanged(object sender, ThueSuatIdChangedEventArgs e)
        {

            System.Windows.Data.CollectionViewSource dm_ThueSuat_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_ThueSuat_ListViewModelSource"];
            DM_ThueSuat_ListViewModel dm_ThueSuat_ListViewModel = ((List<object>)dm_ThueSuat_CollectionViewSource.Source)[0] as DM_ThueSuat_ListViewModel;

            BSP_BanDichVu_ThueChild ThueChild = GetBanDichVuViewModel().Model.BSP_BanDichVu_Thues[e.Index];
            IList<DM_ThueSuatInfo> list = dm_ThueSuat_ListViewModel.Model.getListOfInfo();
            foreach (DM_ThueSuatInfo item in list)
            {
                if (item.Khoa == e.ThueSuatId)
                {
                    ThueChild.ThueSuatValue = (double)item.GiaTriTinhThue;
                }
            }
        }

        void bsp_BanHangHoa_HangHoaIdChanged(object sender, HangHoaIdChangedEventArgs e)
        {

            BSP_BanHangHoa_ChiTietChild ChiTietChild = GetBanHangHoaViewModel().Model.BSP_BanHangHoa_ChiTiets[e.Index];
            BSP_BanHangHoa_ThueChild ThueChild = GetBanHangHoaViewModel().Model.BSP_BanHangHoa_Thues[e.Index];
            BSP_BanHangHoa_PhuPhiChild PhuPhiChild = GetBanHangHoaViewModel().Model.BSP_BanHangHoa_PhuPhis[e.Index];
            BSP_BanHangHoa_GiaVonChild GiaVonChild = GetBanHangHoaViewModel().Model.BSP_BanHangHoa_GiaVons[e.Index];


            System.Windows.Data.CollectionViewSource dm_HangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_HangHoa_ListViewModelSource"];
            DM_HangHoa_ListViewModel dm_HangHoa_ListViewModel = ((List<object>)dm_HangHoa_CollectionViewSource.Source)[0] as DM_HangHoa_ListViewModel;
            System.Collections.Generic.IList<DM_HangHoaInfo> list = dm_HangHoa_ListViewModel.Model.getListOfInfo();

            System.Windows.Data.CollectionViewSource dm_TaiKhoan_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_TaiKhoan_ListViewModelSource"];
            DM_TaiKhoan_ListViewModel dm_TaiKhoan_ListViewModel = ((List<object>)dm_TaiKhoan_CollectionViewSource.Source)[0] as DM_TaiKhoan_ListViewModel;
            System.Collections.Generic.IList<DM_TaiKhoanInfo> listTaiKhoan = dm_TaiKhoan_ListViewModel.Model.getListOfInfo();



            foreach (DM_HangHoaInfo item in list)
            {
                if (item.Khoa == e.HangHoaId)
                {
                    ChiTietChild.DienGiai = item.TenViet;
                    ChiTietChild.KhoId = item.KhoNgamDinhId;

                    ChiTietChild.DonGia = item.DonGiaBan;
                    ChiTietChild.TKNoId = listTaiKhoan[0].Khoa;
                    ChiTietChild.TKCoId = (int)item.TKKhoId;
                    ChiTietChild.DVTId = item.DonViTinhId;

                    ThueChild.HangHoaId = item.Khoa;
                    ThueChild.DienGiai = item.TenViet;
                    ThueChild.ThueSuatId = item.ThueSuatId;
                    ThueChild.TKNoId = listTaiKhoan[0].Khoa;
                    ThueChild.TKCoId = ChiTietChild.TKCoId;

                    PhuPhiChild.HangHoaId = item.Khoa;
                    PhuPhiChild.DienGiai = item.TenViet;

                    GiaVonChild.HangHoaId = item.Khoa;
                    GiaVonChild.DienGiai = item.TenViet;
                    GiaVonChild.TKNoId = listTaiKhoan[0].Khoa;
                    GiaVonChild.TKCoId = listTaiKhoan[0].Khoa;

                    break;
                }
            }
        }

        void bsp_BanDichVu_HangHoaIdChanged(object sender, HangHoaIdChangedEventArgs e)
        {
        }
        void dm_LoaiTienTe_ViewModel_Saved(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource dm_LoaiTienTe_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_LoaiTienTe_ListViewModelSource"];
            DM_LoaiTienTe_ListViewModel vm = ((List<object>)dm_LoaiTienTe_ListViewSource.Source)[0] as DM_LoaiTienTe_ListViewModel;
            vm.GetDM_LoaiTienTe_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
        }

     

        void TM_PhieuThu_ThueSuatIdChanged(object sender, ThueSuatIdChangedEventArgs e)
        {
            
            System.Windows.Data.CollectionViewSource dm_ThueSuat_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_ThueSuat_ListViewModelSource"];
            DM_ThueSuat_ListViewModel dm_ThueSuat_ListViewModel = ((List<object>)dm_ThueSuat_CollectionViewSource.Source)[0] as DM_ThueSuat_ListViewModel;    
            
            TM_PhieuThu_ThueChild ThueChild = GetViewModel().Model.TM_PhieuThu_Thues[e.Index];
            IList<DM_ThueSuatInfo> list = dm_ThueSuat_ListViewModel.Model.getListOfInfo();
            foreach (DM_ThueSuatInfo item in list)
            {
                if (item.Khoa == e.ThueSuatId)
                {
                    ThueChild.ThueSuatValue = (double) item.GiaTriTinhThue;
                }
            }

           

        }
        void TM_PhieuThu_TinhChatHangHoaChanged(object sender, EventArgs e)
        {
            int? tinhChatHangHoaId = this.GetViewModel().Model.TinhChatHangHoaId;
            
            System.Windows.Data.CollectionViewSource dm_HangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_HangHoa_ListViewModelSource"];
            DM_HangHoa_ListViewModel dm_HangHoa_ListViewModel = ((List<object>)dm_HangHoa_CollectionViewSource.Source)[0] as DM_HangHoa_ListViewModel;
            dm_HangHoa_ListViewModel.GetDM_HangHoa((int)tinhChatHangHoaId);
              
           
        }

        void TM_PhieuThu_LoaiTienTeChanged(object sender, EventArgs e)
        {
            
            System.Windows.Data.CollectionViewSource dm_LoaiTienTe_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_LoaiTienTe_ListViewModelSource"];
            DM_LoaiTienTe_ListViewModel dm_LoaiTienTe_ListViewModel = ((List<object>)dm_LoaiTienTe_CollectionViewSource.Source)[0] as DM_LoaiTienTe_ListViewModel;
            IList<DM_LoaiTienTeInfo> list = dm_LoaiTienTe_ListViewModel.Model.getListOfInfo();
            foreach( DM_LoaiTienTeInfo item in list)
            {
                if (item.Khoa == GetViewModel().Model.LoaiTienId)
                {
                    GetViewModel().Model.TyGia = item.TyGiaQuyDoi;
                    break;
                }
            }
        }
       
        
        void TM_PhieuThu_HangHoaIdChanged(object sender, HangHoaIdChangedEventArgs e)
        {
           
            TM_PhieuThu_ChiTietChild ChiTietChild =  GetViewModel().Model.TM_PhieuThu_ChiTiets[e.Index];
            TM_PhieuThu_ThueChild ThueChild =  GetViewModel().Model.TM_PhieuThu_Thues[e.Index];
            
            System.Windows.Data.CollectionViewSource dm_HangHoa_CollectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_HangHoa_ListViewModelSource"];
            DM_HangHoa_ListViewModel dm_HangHoa_ListViewModel = ((List<object>)dm_HangHoa_CollectionViewSource.Source)[0] as DM_HangHoa_ListViewModel;
            System.Collections.Generic.IList<DM_HangHoaInfo> list = dm_HangHoa_ListViewModel.Model.getListOfInfo();

                
            foreach (DM_HangHoaInfo item in list)
            {
                if (item.Khoa == e.HangHoaId)
                {
                    ChiTietChild.DienGiai = item.TenViet;
                    ChiTietChild.SoTien = (double) item.DonGiaBan;
                    ThueChild.DienGiai = item.TenViet;
                    ThueChild.ThueSuatId = item.ThueSuatId;    
                    break;
                }
            }

        }

        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            if (this.tabControl.Visibility == Visibility.Visible)
            {
                if (this.GetViewModel().CanSave)
                {
                    this.GetViewModel().SavePhieuThu();
                }
            }
            else if (this.BanHangHoaTabControl.Visibility == Visibility.Visible)
            {

                if (this.GetViewModel().CanSave)
                {
                    this.GetViewModel().SavePhieuThu();
                }
                if (GetBanHangHoaViewModel().CanSave)
                {
                    GetBanHangHoaViewModel().SaveBanHangHoa();
                }

            }
            else if (this.BanDichVuTabControl.Visibility == Visibility.Visible)
            {

                if (this.GetViewModel().CanSave)
                {
                    this.GetViewModel().SavePhieuThu();
                }
                if (GetBanDichVuViewModel().CanSave)
                {
                    GetBanDichVuViewModel().SaveBanDichVu();
                }

            }
        }
       

        private void AddPhieuThuDetailsItem()
        {
            GetViewModel().AddTM_PhieuThu_ChiTietItem();
        }

        private void AddBanDichVuDetailsItem()
        {
            GetBanDichVuViewModel().AddBSP_BanDichVu_ChiTietItem();
        }

        private void DeleteBanDichVuDetailsItem(int Index)
        {
            GetBanDichVuViewModel().DeleteBSP_BanDichVu_ChiTietItem(Index);
        }

        private void AddBanHangHoaDetailsItem()
        {
            GetBanHangHoaViewModel().AddBSP_BanHangHoa_ChiTietItem();
        }

        private void DeleteBanHangHoaDetailsItem(int Index)
        {
            GetBanHangHoaViewModel().DeleteBSP_BanHangHoa_ChiTietItem(Index);
        }
        

        private void DeletePhieuThuDetailsItem(int Index)
        {
            GetViewModel().DeleteTM_PhieuThu_ChiTietItem( Index);
        }

        void TM_PhieuThu_ViewModel_ChildList_AddedNew(object sender, ChildListAddedNewArgs e)
        {
            if (e.ChildListName == "TM_PhieuThu_ChiTiets")
            {
                this.TM_PhieuThu_PhieuThuChiTietGridView.OnAddingNewDataItem(new GridViewAddingNewEventArgs(GetActiveGridView()));
            }
            else if (e.ChildListName == "TM_PhieuThu_Thues")
            {
                this.TM_PhieuThu_ThueChiTietGridView.OnAddingNewDataItem(new GridViewAddingNewEventArgs(GetActiveGridView()));
            }
            
        }

        void bsp_BanHangHoa_ViewModel_ChildList_AddedNew(object sender, ChildListAddedNewArgs e)
        {
            if (e.ChildListName == "BSP_BanHangHoa_ChiTiets")
            {
                this.BSP_BanHangHoa_BanHangHoaChiTietGridView.OnAddingNewDataItem(new GridViewAddingNewEventArgs(GetActiveGridView()));
            }
            else if (e.ChildListName == "BSP_BanHangHoa_Thues")
            {
                this.BSP_BanHangHoa_PhuPhiGridView.OnAddingNewDataItem(new GridViewAddingNewEventArgs(GetActiveGridView()));
            }
            else if (e.ChildListName == "BSP_BanHangHoa_PhuPhis")
            {
                this.BSP_BanHangHoa_PhuPhiGridView.OnAddingNewDataItem(new GridViewAddingNewEventArgs(GetActiveGridView()));
            }
            else if (e.ChildListName == "BSP_BanHangHoa_GiaVons")
            {
                this.BSP_BanHangHoa_GiaVonGridView.OnAddingNewDataItem(new GridViewAddingNewEventArgs(GetActiveGridView()));
            }
        }

        void bsp_BanDichVu_ViewModel_ChildList_AddedNew(object sender, ChildListAddedNewArgs e)
        {
            if (e.ChildListName == "BSP_BanDichVu_ChiTiets")
            {
                this.BSP_BanDichVu_BanDichVuChiTietGridView.OnAddingNewDataItem(new GridViewAddingNewEventArgs(GetActiveGridView()));
            }
            else if (e.ChildListName == "BSP_BanDichVu_Thues")
            {
                this.BSP_BanDichVu_PhuPhiGridView.OnAddingNewDataItem(new GridViewAddingNewEventArgs(GetActiveGridView()));
            }
        }
        #endregion
    }
}
